$(function(){
	
	loadTable(null,null,null);
	
	//搜索
	$("#selectBtn").click(function(){
		var shopName = $("#shopName").val();
		var goodsName = $("#goodsName").val();
		var type  = $("#type").val();
		
		loadTable(shopName,goodsName,type);
	});
	
	/**
	 * 移除
	 */
	$("#removeBtn").click(function() {
		var rows = isSelected();
		if (rows !== null) {
			removeGoods(rows);
		}
	});
	
	//推荐
	$("#recommend").click(function(){
		var selected = getSelected();
		if(!selected){
			return false;
		}
		editingIndex = selected.index;
		editingData = selected.row;
		if(editingData.recommend){
			layer.msg("这个商品已经处于推荐状态了。", {
				shift : 6,
				offset : "5%"
			});
			return false;
		}
	 	_editerIndex = layer.open({
			id: "goodsRecommend",
			title: "设置商品推荐",
		    type: 1,
		    skin: "layui-layer-rim",
		    area: ["720px", "300px"],
		    content: 
		    '<div id="goods-recommend" class="form" style="height: 100%;overflow: auto;">' +
				'<input type="hidden" name="id" />' +
				'<input type="hidden" name="recommend" value="1" />' +
				'<input type="hidden" name="submit-url" value="goods/recommend.do" />' +
				'<label class="from-label" for="name">商品名称：</label>' +
				'<input type="text" name="name" allowBlank="false" class="form-control" placeholder="商品名称" readonly="true" />' +
				'<div class="input-box"><label class="from-label" for="recommend_end">结束时间:</label>' +
				'<div name="recommend_end" allowBlank="false" placeholder="结束时间" id="recommend_end" style="display: inline-block;margin:10px 0;vertical-align:middle"></div></div>' +
				'<br>' +
			'</div>' +
			'<div class="form" style="position: absolute;bottom: 0px;width: 100%;text-align: right;background-color: #DDD;">' +
				'<input type="button" class="btn btn-sm btn-success" onclick="submitEditer(this)" value="保存"/>' +
				'<input type="button" class="btn btn-sm btn-info" onclick="closeEditer(this)" value="返回"/>' +
			'</div>',
		});
		$("#recommend_end").jqxDateTimeInput({ width: "200px", height: "34px", formatString: "yyyy-MM-dd", value: null });
	 	var displayData = clone(editingData);
	 	delete displayData.recommend_end;
	 	$("input[name='id']").val(editingData['id']);
	 	setData($("#goods-recommend"), displayData);
	});
	
	
	function loadTable(shopName,goodsName,type){
		$("#dataTable").jqxDataTable({
			width: "100%",
	        source: getSource(shopName,goodsName,type),
	        pageable: true,
	        pageSize: 15,
	        serverProcessing: true,
	        pagerButtonsCount: 10,
	        columnsResize: true,
	        sortable: true,
	        columns: [
	            { text: "序号", width: 50, cellsRenderer: function(row) { return (row + 1).toString(); } },
	            { text: "店铺名", dataField: "tags" },
	            { text: "名称", dataField: "name" },
	            { text: "是否推荐", dataField: "recommend", 
	            	cellsRenderer: function(row, column, value, rowData){
	            		return ["否","是"][value];
	            	}
	            },
	            { text: "结束推荐", dataField: "recommend_end",
	            	cellsRenderer: function(row, column, value, rowData) {
	            		return rowData.recommend ? format(value, "Y-M-D H:I") : " - - ";
	            	}
	            },
	            { dataField: "logo", hidden: true }
	        ],
		});
	}
	
	function getSource(shopName,goodsName,type){
		var p = "?1=1";
		if(shopName!=undefined&&shopName!=null&&shopName!=""){
			p+="&shopName="+shopName;
		}
		if(goodsName!=undefined&&goodsName!=null&&goodsName!=""){
			p+="&goodsName="+goodsName;
		}
		if(type!=undefined&&type!=null&&type!=""&&type!=0){
			p+="&type="+type;
		}
		
		var tableSource = new $.jqx.dataAdapter({
			dataType: "json",
			dataFields:[
			    { name: "id", type: "int" },
			    { name: "uuid", type: "string" },
			    { name: "tags", type: "string" },
			    { name: "name", type: "string" },
			    { name: "show", type: "string"},
			    { name: "recommend", type: "int"},
			    { name: "recommend_end", type: "date"}
			],
			id: "id",
			url: "goods/getGoodsList.do"+p
		});
		return tableSource;
	}
	
	/**
	 * 移除选中的商品
	 * 
	 * @param uuids
	 */
	function removeGoods(rows) {
		var uuids = new Array();
		$.each(rows, function(i) {
			uuids[i] = rows[i].uuid;
		});
		layer.confirm('确定移除推荐吗？', {
			btn : [ '确定', '放弃' ]
		// 按钮
		}, function() {
			
			/**
			 * 提交移除请求
			 */
			$.get("/manager/goods/removeRecommend.do",{
				uuids:uuids
			}, function(result) {
				layer.msg(result.msg);
				if (result.code === 1) {
					for (i = rows.length - 1; i >= 0; i--) {
						var row = rows[i];
						for (j = 0; j < uuids.length; j++) {
							if (row.uuid === uuids[j]) {
								$("#dataTable").jqxDataTable('deleteRow', i);
							}
						}
					}

				}
			});
		});
	}
	
	/**
	 * 是否选中一项
	 * 
	 * @returns
	 */
	function isSelected() {
		var rows = $("#dataTable").jqxDataTable("getSelection");
		if (rows.length < 1) {
			layer.msg("请选择一行数据", {
				shift : 6,
				offset : "5%"
			});
			return null;
		}
		return rows;
	}
});